﻿using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using DimensionsHelper.Common.Utils;

namespace DimensionsHelper.ExcelAddIn.Core;

public class Transimission
{

    private Transimission(string type, string data)
    {
        Type = type;
        Data = data;
    }


    private Transimission(string type)
    {
        Type = type;
        Data = null;
    }


    public Transimission()
    {
        Type = string.Empty;
        Data = null;
    }


    public string Type { get; set; }

    public object? Data { get; set; }

    
    public static Transimission Create<T>(string type, T data, JsonTypeInfo<T> jsonTypeInfo)
    {
        return new Transimission(type, JsonHelper.Serialize(data, jsonTypeInfo));
    }


    public static Transimission Create(string type, object? data)
    {
        return new Transimission(type) { Data = data };
    }


    public string ToJson()
    {
        return JsonHelper.Serialize(this, JsonContext.Default.Transimission);
    }


    public bool IsStringData(out string data)
    {
        if (Data is JsonElement element && element.ValueKind == JsonValueKind.String)
        {
            data = element.GetString() ?? "";
            return true;
        }

        data = "";
        return false;
    }

}
